package com.happyass.service.impl;

import com.happyass.dao.StaffDao;
import com.happyass.pojo.Staff;
import com.happyass.pojo.User;
import com.happyass.service.StaffInfoService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

@Service
public class StaffInfoServiceImpl implements StaffInfoService {

    @Autowired
    StaffDao staffDao;

    @Override
    public HashMap<String, Object> login(Staff staff, HttpServletRequest request) {
        HashMap<String,Object> map = new HashMap<String,Object>();
        Staff staff1 =  staffDao.findByStaffUsername(staff.getStaffUsername());
        if(staff1!=null){
            try {
                UsernamePasswordToken token = new UsernamePasswordToken(staff.getStaffUsername(), staff.getStaffPwd());
                Subject subject = SecurityUtils.getSubject();
                subject.login(token);
                subject.checkPermissions();
                subject.checkRoles();
                HttpSession session = request.getSession();
                Staff staff2 = (Staff) subject.getPrincipal();
                session.setAttribute("staffId",staff2.getStaffId());
                map.put("info", true);
            } catch (IncorrectCredentialsException e) {
                map.put("info","密码输入错误");
            }
        }else {
            map.put("info","账号不存在");
        }
       return  map;

    }



    @Override
    public List<Staff> getStaffInfos() {
        return staffDao.getStaffInfos();
    }

    @Override
    public Staff getStaffByStaffUserName(String staffUsername) {
        return staffDao.findByStaffUsername(staffUsername);
    }

    @Override
    public boolean add(Staff staff)  {
        try {
            Staff staffInfo = new Staff();
            staffInfo.setStaffUsername(staff.getStaffUsername());
            staffInfo.setStaffPwd(staff.getStaffPwd());
            staffInfo.setStaffAddr(staff.getStaffAddr());
            staffInfo.setStaffName(staff.getStaffName());
            staffInfo.setStaffTel(staff.getStaffTel());
            staffDao.save(staffInfo);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public boolean update(Staff staff) {
        try {
            Staff info = staffDao.findById(staff.getStaffId()).get();
            info.setStaffName(staff.getStaffName());
            info.setStaffUsername(staff.getStaffUsername());
            info.setStaffAddr(staff.getStaffAddr());
            info.setStaffTel(staff.getStaffTel());
            staffDao.save(info);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
            return false;

    }

    @Override
    public boolean del(Integer StaffId) {
        try {
            staffDao.deleteById(StaffId);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public Staff findByStaffId(Integer staffId) {
        return staffDao.findById(staffId).get();
    }

    @Override
    public List<Staff> staffRoles() {
        return staffDao.staffRoles();
    }

    @Override
    public Staff selectStaffRolesByStaffId(Integer staffId) {
        return  staffDao.selectStaffRolesByStaffId(staffId);
    }

    @Override
    public Staff selectById(Integer staffId) {
        return staffDao.findById(staffId).get();
    }

    @Override
    public boolean edit(String currentPassword, String newPassword) {
        //查询要修改的用户对象
        Subject subject = SecurityUtils.getSubject();
        Staff staffTemp = (Staff) subject.getPrincipal();
        String staffPwd = staffTemp.getStaffPwd();
        if (currentPassword.equals(staffPwd) ) {
            staffTemp.setStaffPwd(newPassword);
            //修改保存
            staffDao.save(staffTemp);
            return true;
        } else {
            return false;
        }
    }
}
